﻿using ESRI.ArcGIS.AnalysisTools;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geoprocessor;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace AEFunction.UI
{
    public partial class BatchClip : Form
    {
        private ToolStripProgressBar pb1;
        private List<ILayer> layers;
        public List<string[]> outputLayerPaths = new List<string[]>();//裁剪后的图层的路径
        public BatchClip(ToolStripProgressBar p, List<ILayer> l)
        {
            pb1 = p;
            layers = l;
            InitializeComponent();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Geoprocessor GP = new Geoprocessor();
            Clip cliper = new Clip();
            cliper.clip_features = GetFeatureLayerFromShapeFile(ClipLayersTxt.Text);
            pb1.Maximum = InputLayerTxt.Text.Split(';').Length + 1;
            foreach (string s in InputLayerTxt.Text.Split(';'))
            {
                string[] array = s.Split('\\');
                string fileName = array[array.Length - 1];
                cliper.in_features = GetFeatureLayerFromShapeFile(s);
                cliper.out_feature_class = OutputTxt.Text + "\\" + fileName;
                cliper.cluster_tolerance = 0;
                GP.Execute(cliper, null);
                string[] outputLayerPath = new string [] { OutputTxt.Text, fileName };
                outputLayerPaths.Add(outputLayerPath);
                pb1.Value++;
            }
         
            this.DialogResult = DialogResult.OK;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            InputLayerTxt.Text=GetFilePath( true, "ShapeFiles文件(*.shp)|*.shp|所有文件(*.*)|*.*");
        }


        private void button2_Click(object sender, EventArgs e)
        {
            ClipLayersTxt.Text = GetFilePath( false, "ShapeFiles文件(*.shp)|*.shp|所有文件(*.*)|*.*"); 
        }

        private void button3_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                OutputTxt.Text = fbd.SelectedPath;
            }
        }

        private string GetFilePath(bool mutiSelect, string filter)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Multiselect = mutiSelect;
            string file = "";
            ofd.Filter = filter;
            if (ofd.ShowDialog() == DialogResult.OK)
            {

                file = ofd.FileNames[0];
                if (mutiSelect == true)
                {
                    for (int i = 1; i < ofd.FileNames.Length; i++)
                    {
                        file += ";" + ofd.FileNames[i];
                    }
                }
            }
            return file;
        }


        private IFeatureLayer GetFeatureLayerFromShapeFile(string path)
        {
            int Position = path.LastIndexOf("\\");
            string FilePath = path.Substring(0, Position);
            string ShpName = path.Substring(Position + 1);
            IWorkspaceFactory factory = new ShapefileWorkspaceFactoryClass();
            IWorkspace workspace = factory.OpenFromFile(FilePath, 0);
            IFeatureWorkspace featureworkspace = workspace as IFeatureWorkspace;
            IFeatureClass featureclass = featureworkspace.OpenFeatureClass(ShpName);
            IFeatureLayer layer = new FeatureLayerClass();
            layer.FeatureClass = featureclass;
            layer.Name = ShpName.Split('.')[0];
            return layer;
        }
    }
}
